//
//  HNSH_AlertView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#import "HNSH_AlertView.h"
#import "HNSH_PopupController.h"

@interface HNSH_AlertView ()

/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *hhzxPopView;
/* okButton */
@property (nonatomic, strong) UIButton *okButton;
/* cancelButton */
@property (nonatomic, strong) UIButton *cancelButton;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;

/* buttonClick */
@property (nonatomic, copy) dispatch_block_t buttonClick;
/* cancelButtonClick */
@property (nonatomic, copy) dispatch_block_t cancelButtonClick;

@end

@implementation HNSH_AlertView

+ (void)showTitle:(NSString *)title des:(NSString *)des buttonTitle:(NSString *)buttonTitle buttonColor:(UIColor *)buttonColor buttonClick:(dispatch_block_t)buttonClick {
    HNSH_AlertView *alert = [[HNSH_AlertView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-80, 160)];
    alert.buttonClick = buttonClick;
    alert.titleLabel.text = title;
    [alert.okButton setTitle:buttonTitle forState:UIControlStateNormal];
    alert.okButton.backgroundColor = buttonColor;
    if ([des isNotBlank]) {
        alert.desLabel.hidden = NO;
        alert.desLabel.text = des;
        alert.okButton.frame = CGRectMake(16, 105, alert.width-32, 40);
    } else {
        alert.cancelButton.hidden = NO;
    }
    [alert showView];
}

+ (void)showTitle:(NSString *)title des:(NSString *)des
      buttonTitle:(NSString *)buttonTitle buttonColor:(UIColor *)buttonColor buttonClick:(dispatch_block_t)buttonClick
cancelButtonTitle:(NSString *)cancelButtonTitle cancelButtonColor:(UIColor *)cancelButtonColor cancelButtonClick:(dispatch_block_t)cancelButtonClick {
    HNSH_AlertView *alert = [[HNSH_AlertView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-80, 200)];
    alert.buttonClick = buttonClick;
    alert.titleLabel.text = title;
    alert.titleLabel.frame = CGRectMake(16, 28, alert.width-32, 50);
    alert.titleLabel.numberOfLines = 2;
    [alert.okButton setTitle:buttonTitle forState:UIControlStateNormal];
    alert.okButton.backgroundColor = buttonColor;
    alert.okButton.frame = CGRectMake(32+(alert.width-48)/2, 136, (alert.width-48)/2, 40);
    
    alert.cancelButtonClick = cancelButtonClick;
    alert.cancelButton.hidden = NO;
    [alert.cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
    alert.cancelButton.backgroundColor = cancelButtonColor;
    alert.cancelButton.frame = CGRectMake(16, 136, (alert.width-48)/2, 40);
    
    if ([des isNotBlank]) {
        alert.desLabel.hidden = NO;
        alert.desLabel.text = des;
        alert.desLabel.frame = CGRectMake(16, 88, alert.width-32, 20);
    }
    [alert showView];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addSubview:self.titleLabel];
    [self addSubview:self.desLabel];
    [self addSubview:self.okButton];
    [self addSubview:self.cancelButton];
}

- (void)showView {
    self.hhzxPopView = [[HNSH_PopupController alloc]init];
    self.hhzxPopView.dismissOnMaskTouched = NO;
    self.hhzxPopView.layoutType = PopupLayoutTypeCenter;
    self.hhzxPopView.slideStyle = PopupSlideStyleFade;
    [self.hhzxPopView presentContentView:self duration:0.25 springAnimated:YES inView:kSTWindow];
    self.hhzxPopView.popupView.layer.cornerRadius = 10.0f;
    self.hhzxPopView.popupView.layer.masksToBounds = YES;
}

- (void)closeView {
    [self.hhzxPopView dismissWithDuration:0.25 springAnimated:YES];
}

#pragma mark lazy
- (UIButton *)okButton {
    if (!_okButton) {
        _okButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _okButton.frame = CGRectMake(32+(self.width-48)/2, 94, (self.width-48)/2, 40);
        [_okButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _okButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _okButton.layer.cornerRadius = 8.0f;
        _okButton.layer.masksToBounds = YES;
        WeakSelf(weakSelf);
        [[_okButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
            if (weakSelf.buttonClick) {
                weakSelf.buttonClick();
            }
        }];
    }
    return _okButton;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.frame = CGRectMake(16, 94, (self.width-48)/2, 40);
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _cancelButton.backgroundColor = [UIColor appWhiteLabelColor];
        _cancelButton.layer.cornerRadius = 8.0f;
        _cancelButton.layer.masksToBounds = YES;
        _cancelButton.layer.borderColor = [[UIColor colorWithHexString:@"#E3E3E3"] CGColor];
        _cancelButton.layer.borderWidth = 1.0f;
        _cancelButton.hidden = YES;
        WeakSelf(weakSelf);
        [[_cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
        }];
    }
    return _cancelButton;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 36, self.width-32, 22)];
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 40, self.width-32, 60)];
        _desLabel.textColor = [UIColor appGrayLabelColor];
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.textAlignment = NSTextAlignmentCenter;
        _desLabel.hidden = YES;
    }
    return _desLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
